package sophix.example.ronny.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bmob.initialize(MainActivity.this,"9f0edd338e12c67aeedb1bf4d79c0802");

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Student student = new Student();
                student.setStuno("171004");
                student.setName("李四");
                student.setAge(20);
                student.setAddress("广州市天河区");
                student.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if(e == null){
                            Toast.makeText(MainActivity.this, "保存成功",
                            Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Teacher teacher = new Teacher();
                teacher.setName("张凯");
                teacher.setAge(34);
                teacher.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if(e == null){
                            Toast.makeText(MainActivity.this, "保存成功",
                                    Toast.LENGTH_SHORT).show();
                        }else{
                            Log.e("BOMB", e.getMessage());
                        }
                    }
                });
            }
        });

        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                          if(e == null){
                              StringBuffer buffer = new StringBuffer();
                              for(Student student : list){
                                  buffer.append(student.getStuno())
                                          .append(student.getName())
                                          .append(student.getAge())
                                          .append(student.getAddress())
                                          .append("\n");
                              }
                              Toast.makeText(MainActivity.this, buffer.toString(), Toast.LENGTH_LONG).show();
                          }
                    }
                });
            }
        });

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
                studentBmobQuery.addWhereEqualTo("name","王增辉");
                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                        if(e == null){
                            StringBuffer buffer = new StringBuffer();
                            for(Student student : list){
                                buffer.append(student.getStuno())
                                        .append(student.getName())
                                        .append(student.getAge())
                                        .append(student.getAddress())
                                        .append("\n");
                            }
                            Toast.makeText(MainActivity.this, buffer.toString(), Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        });

        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
                studentBmobQuery.addWhereEqualTo("name","王增辉");
                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                        if(e == null){
                            Student student =  list.get(0);
                            student.delete(new UpdateListener() {
                                @Override
                                public void done(BmobException e) {
                                    Toast.makeText(MainActivity.this, "删除成功",
                                            Toast.LENGTH_SHORT).show();
                                }
                            });
                            }
                    }
                });
                /*
                下面这种直接用名字删除是无效的
                Student student =  new Student();
                student.setName("王增辉");
                student.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e == null){

                            Toast.makeText(MainActivity.this, "删除成功",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });*/
            }
        });


        findViewById(R.id.btn6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
                studentBmobQuery.addWhereEqualTo("name","李四");
                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                        if(e == null  && list.size() > 0){
                            Student student =  list.get(0);
                            student.setAge(33);
                            
                            student.update(new UpdateListener() {
                                @Override
                                public void done(BmobException e) {
                                    if(e == null){

                                        Toast.makeText(MainActivity.this, "更新成功",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    }
                });
            }
        });
    }
}
